/**
 * @file pt_utils.c
 * @brief 共用组件
 * @author yejiahao
 * @date 2023-11-14 06:49:26
 * 
 * @copyright Copyright (c) 2023 by 深圳市鹏天微智能科技有限公司, All Rights Reserved.
 */

#include "pt_utils.h"

/*********************
 *    data print
 *********************/

/**
 * @brief 按十进制打印 buffer 数组
 * 
 * @param buffer 
 * @param size 
 */
void pt_print_buffer(uint8_t *buffer, uint16_t size)
{
    for (int i = 0; i < size; i++)
    {
        utils_printf("%d ", buffer[i]);
    }
    utils_printf("\n");
}

/**
 * @brief 按十六进制打印 buffer 数组
 * 
 * @param buffer 
 * @param size 
 */
void pt_print_buffer_hex(uint8_t *buffer, uint16_t size)
{
    for (int i = 0; i < size; i++)
    {
        utils_printf("0x%02X ", buffer[i]);
    }
    utils_printf("\n");
}

/**
 * @brief 按位打印 8位 数据
 * 
 * @param data 
 */
void pt_print_bin_8(uint8_t data)
{
    for (int i = 0; i < 8; i++)
    {
        if ((i > 0) && ((i % 4) == 0))
        {
            utils_printf(" ");
        }
        utils_printf("%d", (data & (1 << (7 - i))) ? 1 : 0);
    }
    utils_printf("\n");
}

/**
 * @brief 按位打印 32位 数据
 * 
 * @param data 
 */
void pt_print_bin_32(uint32_t data)
{
    for (int i = 0; i < 32; i++)
    {
        if ((i > 0) && ((i % 4) == 0))
        {
            utils_printf(" ");
        }
        utils_printf("%d", (data & (1 << (31 - i))) ? 1 : 0);
    }
    utils_printf("\n");
}

/**
 * @brief 对所有的数据帧求和取反+1校验
 * 
 * @param data 
 * @param lenght 
 * @return uint8_t 
 */
uint8_t pt_check_sum(uint8_t *data,uint8_t lenght)
{
    uint8_t sum = 0;
    uint8_t check_sum = 0;
    for (int i = 0; i < lenght; i++)
    {
        sum += data[i];
    }
    check_sum = ~(sum & 0xFF) + 1;
    return check_sum;
}